package com.nengchuan.eic.nceconomy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nengchuan.eic.nceconomy.dao.UserRoleMapper;
import com.nengchuan.eic.nceconomy.entity.po.Role;
import com.nengchuan.eic.nceconomy.dao.RoleMapper;
import com.nengchuan.eic.nceconomy.entity.po.UserRole;
import com.nengchuan.eic.nceconomy.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nsc.boot.sdk.role.model.vo.RoleUserVO;
import com.nsc.boot.sdk.role.model.vo.RoleVo;
import com.nsc.boot.sdk.role.model.vo.UserVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author luoping
 * @since 2020-04-26
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<RoleVo> findRoles(int status){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("status",status);
        List<Role> roles=roleMapper.selectList(queryWrapper);
        List<RoleVo> roleVos=new ArrayList<>(roles.size());
        roles.forEach(e->roleVos.add(RoleVo.builder().roleId(e.getId().toString()).roleName(e.getRoleName()).build()));
        return roleVos;
    }

    @Override
    public List<RoleUserVO> getRoleUsers() {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        List<UserRole> userRoles=userRoleMapper.selectList(queryWrapper);
        List<RoleUserVO> roleUserVOs=new ArrayList<>(userRoles.size());
        userRoles.forEach(e->roleUserVOs.add(RoleUserVO.builder().roleId(e.getRoleId().toString()).userId(e.getUserId().toString()).userName(e.getUserName()).build()));
        return roleUserVOs;
    }

    @Override
    public List<UserVo> getUsers(Long roleId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        List<UserRole> userRoles=userRoleMapper.selectList(queryWrapper);
        List<UserVo> userVOs=new ArrayList<>(userRoles.size());
        userRoles.forEach(e->userVOs.add(UserVo.builder().userId(e.getUserId().toString()).userName(e.getUserName()).build()));
        return userVOs;
    }
}
